{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 项目1-PM2.5预测\n",
    "\n",
    "## 项目描述\n",
    "* 本次作业的资料是从行政院环境环保署空气品质监测网所下载的观测资料。\n",
    "* 希望大家能在本作业实现 linear regression 预测出 PM2.5 的数值。\n",
    "\n",
    "## 数据集介绍\n",
    "* 本次作业使用丰原站的观测记录，分成 train set 跟 test set，train set 是丰原站每个月的前 20 天所有资料。test set 则是从丰原站剩下的资料中取样出来。\n",
    "* train.csv: 每个月前 20 天的完整资料。\n",
    "* test.csv : 从剩下的资料当中取样出连续的 10 小时为一笔，前九小时的所有观测数据当作 feature，第十小时的 PM2.5 当作 answer。一共取出 240 笔不重複的 test data，请根据 feature 预测这 240 笔的 PM2.5。\n",
    "* Data 含有 18 项观测数据 AMB_TEMP, CH4, CO, NHMC, NO, NO2, NOx, O3, PM10, PM2.5, RAINFALL, RH, SO2, THC, WD_HR, WIND_DIREC, WIND_SPEED, WS_HR。  \n",
    "\n",
    "## 项目要求\n",
    "- 请手动实现 linear regression，方法限使用 gradient descent。\n",
    "- 禁止使用 numpy.linalg.lstsq\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "!pip install --upgrade pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = pd.read_csv('work/hw1_data/train.csv', encoding = 'big5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2.3\n"
     ]
    }
   ],
   "source": [
    "print(pd.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **预处理** \n",
    "取需要的数值部分，将 'RAINFALL' 栏位全部补 0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = data.iloc[:, 3:]\n",
    "data[data == 'NR'] = 0\n",
    "raw_data = data.to_numpy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **提取特征 (1)**\n",
    "![图片说明](https://ai-studio-static-online.cdn.bcebos.com/8dae829615d54e8f8a002bc59330734e24ae4fd8d30c48249c9fd89d4c8452b3)\n",
    "![图片说明](https://ai-studio-static-online.cdn.bcebos.com/271f6ec86c794658b92cb93156b5674d746426dfd699444db0c8aa340f595caf)\n",
    "\n",
    "将原始 4320 * 18 的资料依照每个月份重组成 12 个 18 (特征) * 480 (小时) 的资料。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "month_data = {}\n",
    "for month in range(12):\n",
    "    sample = np.empty([18, 480])\n",
    "    for day in range(20):\n",
    "        sample[:, day * 24 : (day + 1) * 24] = raw_data[18 * (20 * month + day) : 18 * (20 * month + day + 1), :]\n",
    "    month_data[month] = sample"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **提取特征 (2)**\n",
    "![alt text](https://ai-studio-static-online.cdn.bcebos.com/b331ac245ba04847b299b3fbf00208ee66ba7e922d354a9a8d2eb223491d4f05)\n",
    "![alt text](https://ai-studio-static-online.cdn.bcebos.com/aa9319162fda4ab5bc989211167d853a93f63173efb5482bab4cf206b764f7e1)\n",
    "\n",
    "每个月会有 480小时，每 9 小时形成一个数据，每个月会有 471 个数据，故总资料数为 471 * 12 笔，而每笔 数据 有 9 * 18 的 特征 (一小时 18 个 特征 * 9 小时)。\n",
    "\n",
    "对应的 目标 则有 471 * 12 个(第 10 个小时的 PM2.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14.  14.  14.  ...  2.   2.   0.5]\n",
      " [14.  14.  13.  ...  2.   0.5  0.3]\n",
      " [14.  13.  12.  ...  0.5  0.3  0.8]\n",
      " ...\n",
      " [17.  18.  19.  ...  1.1  1.4  1.3]\n",
      " [18.  19.  18.  ...  1.4  1.3  1.6]\n",
      " [19.  18.  17.  ...  1.3  1.6  1.8]]\n",
      "[[30.]\n",
      " [41.]\n",
      " [44.]\n",
      " ...\n",
      " [17.]\n",
      " [24.]\n",
      " [29.]]\n"
     ]
    }
   ],
   "source": [
    "x = np.empty([12 * 471, 18 * 9], dtype = float)\n",
    "y = np.empty([12 * 471, 1], dtype = float)\n",
    "for month in range(12):\n",
    "    for day in range(20):\n",
    "        for hour in range(24):\n",
    "            if day == 19 and hour > 14:\n",
    "                continue\n",
    "            x[month * 471 + day * 24 + hour, :] = month_data[month][:,day * 24 + hour : day * 24 + hour + 9].reshape(1, -1) #vector dim:18*9 (9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9)\n",
    "            y[month * 471 + day * 24 + hour, 0] = month_data[month][9, day * 24 + hour + 9] #value\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **归一化**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.35825331, -1.35883937, -1.359222  , ...,  0.26650729,\n",
       "         0.2656797 , -1.14082131],\n",
       "       [-1.35825331, -1.35883937, -1.51819928, ...,  0.26650729,\n",
       "        -1.13963133, -1.32832904],\n",
       "       [-1.35825331, -1.51789368, -1.67717656, ..., -1.13923451,\n",
       "        -1.32700613, -0.85955971],\n",
       "       ...,\n",
       "       [-0.88092053, -0.72262212, -0.56433559, ..., -0.57693779,\n",
       "        -0.29644471, -0.39079039],\n",
       "       [-0.7218096 , -0.56356781, -0.72331287, ..., -0.29578943,\n",
       "        -0.39013211, -0.1095288 ],\n",
       "       [-0.56269867, -0.72262212, -0.88229015, ..., -0.38950555,\n",
       "        -0.10906991,  0.07797893]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_x = np.mean(x, axis = 0) #18 * 9 \n",
    "std_x = np.std(x, axis = 0) #18 * 9 \n",
    "for i in range(len(x)): #12 * 471\n",
    "    for j in range(len(x[0])): #18 * 9 \n",
    "        if std_x[j] != 0:\n",
    "            x[i][j] = (x[i][j] - mean_x[j]) / std_x[j]\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **将训练数据分割成 \"训练集 \"和 \"验证集\"**\n",
    "这部分是简单示范，以生成比较中用来训练的训练集和不会被放入训练、只是用来验证的验证集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.35825331 -1.35883937 -1.359222   ...  0.26650729  0.2656797\n",
      "  -1.14082131]\n",
      " [-1.35825331 -1.35883937 -1.51819928 ...  0.26650729 -1.13963133\n",
      "  -1.32832904]\n",
      " [-1.35825331 -1.51789368 -1.67717656 ... -1.13923451 -1.32700613\n",
      "  -0.85955971]\n",
      " ...\n",
      " [ 0.86929969  0.70886668  0.38952809 ...  1.39110073  0.2656797\n",
      "  -0.39079039]\n",
      " [ 0.71018876  0.39075806  0.07157353 ...  0.26650729 -0.39013211\n",
      "  -0.39079039]\n",
      " [ 0.3919669   0.07264944  0.07157353 ... -0.38950555 -0.39013211\n",
      "  -0.85955971]]\n",
      "[[30.]\n",
      " [41.]\n",
      " [44.]\n",
      " ...\n",
      " [ 7.]\n",
      " [ 5.]\n",
      " [14.]]\n",
      "[[ 0.07374504  0.07264944  0.07157353 ... -0.38950555 -0.85856912\n",
      "  -0.57829812]\n",
      " [ 0.07374504  0.07264944  0.23055081 ... -0.85808615 -0.57750692\n",
      "   0.54674825]\n",
      " [ 0.07374504  0.23170375  0.23055081 ... -0.57693779  0.54674191\n",
      "  -0.1095288 ]\n",
      " ...\n",
      " [-0.88092053 -0.72262212 -0.56433559 ... -0.57693779 -0.29644471\n",
      "  -0.39079039]\n",
      " [-0.7218096  -0.56356781 -0.72331287 ... -0.29578943 -0.39013211\n",
      "  -0.1095288 ]\n",
      " [-0.56269867 -0.72262212 -0.88229015 ... -0.38950555 -0.10906991\n",
      "   0.07797893]]\n",
      "[[13.]\n",
      " [24.]\n",
      " [22.]\n",
      " ...\n",
      " [17.]\n",
      " [24.]\n",
      " [29.]]\n",
      "4521\n",
      "4521\n",
      "1131\n",
      "1131\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "x_train_set = x[: math.floor(len(x) * 0.8), :]\n",
    "y_train_set = y[: math.floor(len(y) * 0.8), :]\n",
    "x_validation = x[math.floor(len(x) * 0.8): , :]\n",
    "y_validation = y[math.floor(len(y) * 0.8): , :]\n",
    "print(x_train_set)\n",
    "print(y_train_set)\n",
    "print(x_validation)\n",
    "print(y_validation)\n",
    "print(len(x_train_set))\n",
    "print(len(y_train_set))\n",
    "print(len(x_validation))\n",
    "print(len(y_validation))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **训练**\n",
    "![alt text](https://ai-studio-static-online.cdn.bcebos.com/1a34083d8bde48b5b0e09248e38fbd8663509f79b79a48d7bc23d0e32941ebed)\n",
    "![alt text](https://ai-studio-static-online.cdn.bcebos.com/a453c7b4d72444fc92dfde3efd96b1b9d3f7b81f7d3f4da7a870e9b1aba558e1)\n",
    "![alt text](https://ai-studio-static-online.cdn.bcebos.com/bd3836fbb0fe4674933463d56e5be5f0436c8e5e62b044b9a7ac8aeac714054c)\n",
    "\n",
    "(和上图不同处: 下面的代码采用均方根误差)\n",
    "\n",
    "因为常数项的存在，所以 维度 (dim) 需要多加一栏；eps 项是避免 adagrad 的分母为 0 而加的极小数值。\n",
    "\n",
    "每一个 维度 (dim) 会对应到各自的梯度, 权重 (w)，透过一次次的迭代 (iter_time) 学习。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:27.071214829194115\n",
      "100:33.78905859777454\n",
      "200:19.913751298197095\n",
      "300:13.531068193689693\n",
      "400:10.645466158446172\n",
      "500:9.277353455475065\n",
      "600:8.518042045956502\n",
      "700:8.014061987588425\n",
      "800:7.636756824775692\n",
      "900:7.336563740371125\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 2.13740269e+01],\n",
       "       [ 3.58888909e+00],\n",
       "       [ 4.56386323e+00],\n",
       "       [ 2.16307023e+00],\n",
       "       [-6.58545223e+00],\n",
       "       [-3.38885580e+01],\n",
       "       [ 3.22235518e+01],\n",
       "       [ 3.49340354e+00],\n",
       "       [-4.60308671e+00],\n",
       "       [-1.02374754e+00],\n",
       "       [-3.96791501e-01],\n",
       "       [-1.06908800e-01],\n",
       "       [ 2.22488184e-01],\n",
       "       [ 8.99634117e-02],\n",
       "       [ 1.31243105e-01],\n",
       "       [ 2.15894989e-02],\n",
       "       [-1.52867263e-01],\n",
       "       [ 4.54087776e-02],\n",
       "       [ 5.20999235e-01],\n",
       "       [ 1.60824213e-01],\n",
       "       [-3.17709451e-02],\n",
       "       [ 1.28529025e-02],\n",
       "       [-1.76839437e-01],\n",
       "       [ 1.71241371e-01],\n",
       "       [-1.31190032e-01],\n",
       "       [-3.51614451e-02],\n",
       "       [ 1.00826192e-01],\n",
       "       [ 3.45018257e-01],\n",
       "       [ 4.00130315e-02],\n",
       "       [ 2.54331382e-02],\n",
       "       [-5.04425219e-01],\n",
       "       [ 3.71483018e-01],\n",
       "       [ 8.46357671e-01],\n",
       "       [-8.11920428e-01],\n",
       "       [-8.00217575e-02],\n",
       "       [ 1.52737711e-01],\n",
       "       [ 2.64915130e-01],\n",
       "       [-5.19860416e-02],\n",
       "       [-2.51988315e-01],\n",
       "       [ 3.85246517e-01],\n",
       "       [ 1.65431451e-01],\n",
       "       [-7.83633314e-02],\n",
       "       [-2.89457231e-01],\n",
       "       [ 1.77615023e-01],\n",
       "       [ 3.22506948e-01],\n",
       "       [-4.59955256e-01],\n",
       "       [-3.48635358e-02],\n",
       "       [-5.81764363e-01],\n",
       "       [-6.43394528e-02],\n",
       "       [-6.32876949e-01],\n",
       "       [ 6.36624507e-02],\n",
       "       [ 8.31592506e-02],\n",
       "       [-4.45157961e-01],\n",
       "       [-2.34526366e-01],\n",
       "       [ 9.86608594e-01],\n",
       "       [ 2.65230652e-01],\n",
       "       [ 3.51938093e-02],\n",
       "       [ 3.07464334e-01],\n",
       "       [-1.04311239e-01],\n",
       "       [-6.49166901e-02],\n",
       "       [ 2.11224757e-01],\n",
       "       [-2.43159815e-01],\n",
       "       [-1.31285604e-01],\n",
       "       [ 1.09045810e+00],\n",
       "       [-3.97913710e-02],\n",
       "       [ 9.19563678e-01],\n",
       "       [-9.44824150e-01],\n",
       "       [-5.04137735e-01],\n",
       "       [ 6.81272939e-01],\n",
       "       [-1.34494828e+00],\n",
       "       [-2.68009542e-01],\n",
       "       [ 4.36204342e-02],\n",
       "       [ 1.89619513e+00],\n",
       "       [-3.41873873e-01],\n",
       "       [ 1.89162461e-01],\n",
       "       [ 1.73251268e-02],\n",
       "       [ 3.14431930e-01],\n",
       "       [-3.40828467e-01],\n",
       "       [ 4.92385651e-01],\n",
       "       [ 9.29634214e-02],\n",
       "       [-4.50983589e-01],\n",
       "       [ 1.47456584e+00],\n",
       "       [-3.03417236e-02],\n",
       "       [ 7.71229328e-02],\n",
       "       [ 6.38314494e-01],\n",
       "       [-7.93287087e-01],\n",
       "       [ 8.82877506e-01],\n",
       "       [ 3.18965610e+00],\n",
       "       [-5.75671706e+00],\n",
       "       [ 1.60748945e+00],\n",
       "       [ 1.36142440e+01],\n",
       "       [ 1.50029111e-01],\n",
       "       [-4.78389603e-02],\n",
       "       [-6.29463755e-02],\n",
       "       [-2.85383032e-02],\n",
       "       [-3.01562821e-01],\n",
       "       [ 4.12058013e-01],\n",
       "       [-6.77534154e-02],\n",
       "       [-1.00985479e-01],\n",
       "       [-1.68972973e-01],\n",
       "       [ 1.64093233e+00],\n",
       "       [ 1.89670371e+00],\n",
       "       [ 3.94713816e-01],\n",
       "       [-4.71231449e+00],\n",
       "       [-7.42760774e+00],\n",
       "       [ 6.19781936e+00],\n",
       "       [ 3.53986244e+00],\n",
       "       [-9.56245861e-01],\n",
       "       [-1.04372792e+00],\n",
       "       [-4.92863713e-01],\n",
       "       [ 6.31608790e-01],\n",
       "       [-4.85175956e-01],\n",
       "       [ 2.58400216e-01],\n",
       "       [ 9.43846795e-02],\n",
       "       [-1.29323184e-01],\n",
       "       [-3.81235287e-01],\n",
       "       [ 3.86819479e-01],\n",
       "       [ 4.04211627e-01],\n",
       "       [ 3.75568914e-01],\n",
       "       [ 1.83512261e-01],\n",
       "       [-8.01417708e-02],\n",
       "       [-3.10188597e-01],\n",
       "       [-3.96124612e-01],\n",
       "       [ 3.66227853e-01],\n",
       "       [ 1.79488593e-01],\n",
       "       [-3.14477051e-01],\n",
       "       [-2.37611443e-01],\n",
       "       [ 3.97076104e-02],\n",
       "       [ 1.38775912e-01],\n",
       "       [-3.84015069e-02],\n",
       "       [-5.47557119e-02],\n",
       "       [ 4.19975207e-01],\n",
       "       [ 4.46120687e-01],\n",
       "       [-4.31074826e-01],\n",
       "       [-8.74450768e-02],\n",
       "       [-5.69534264e-02],\n",
       "       [-7.23980157e-02],\n",
       "       [-1.39880128e-02],\n",
       "       [ 1.40489658e-01],\n",
       "       [-2.44952334e-01],\n",
       "       [ 1.83646770e-01],\n",
       "       [-1.64135512e-01],\n",
       "       [-7.41216452e-02],\n",
       "       [-9.71414213e-02],\n",
       "       [ 1.98829041e-02],\n",
       "       [-4.46965919e-01],\n",
       "       [-2.63440959e-01],\n",
       "       [ 1.52924043e-01],\n",
       "       [ 6.52532847e-02],\n",
       "       [ 7.06818266e-01],\n",
       "       [ 9.73757051e-02],\n",
       "       [-3.35687787e-01],\n",
       "       [-2.26559165e-01],\n",
       "       [-3.00117086e-01],\n",
       "       [ 1.24185231e-01],\n",
       "       [ 4.18872344e-01],\n",
       "       [-2.51891946e-01],\n",
       "       [-1.29095731e-01],\n",
       "       [-5.57512471e-01],\n",
       "       [ 8.76239582e-02],\n",
       "       [ 3.02594902e-01],\n",
       "       [-4.23463160e-01],\n",
       "       [ 4.89922051e-01]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dim = 18 * 9 + 1\n",
    "w = np.zeros([dim, 1])\n",
    "x = np.concatenate((np.ones([12 * 471, 1]), x), axis = 1).astype(float)\n",
    "learning_rate = 100\n",
    "iter_time = 1000\n",
    "adagrad = np.zeros([dim, 1])\n",
    "eps = 0.0000000001\n",
    "for t in range(iter_time):\n",
    "    loss = np.sqrt(np.sum(np.power(np.dot(x, w) - y, 2))/471/12)#rmse\n",
    "    if(t%100==0):\n",
    "        print(str(t) + \":\" + str(loss))\n",
    "    gradient = 2 * np.dot(x.transpose(), np.dot(x, w) - y) #dim*1\n",
    "    adagrad += gradient ** 2\n",
    "    w = w - learning_rate * gradient / np.sqrt(adagrad + eps)\n",
    "np.save('work/weight.npy', w)\n",
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **测试**\n",
    "![alt text](https://ai-studio-static-online.cdn.bcebos.com/fc702c460628481889782e6a8f6490ce3e892c780e68481dbbb426dc9f459b31)\n",
    "\n",
    "载入测试数据，并且以相似于训练资料预先处理和特徵萃取的方式处理，使测试数据形成 240 个维度为 18 * 9 + 1 的资料。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  after removing the cwd from sys.path.\n",
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/pandas/core/frame.py:3215: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  self._where(-key, value, inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -0.24447681, -0.24545919, ..., -0.67065391,\n",
       "        -1.04594393,  0.07797893],\n",
       "       [ 1.        , -1.35825331, -1.51789368, ...,  0.17279117,\n",
       "        -0.10906991, -0.48454426],\n",
       "       [ 1.        ,  1.5057434 ,  1.34508393, ..., -1.32666675,\n",
       "        -1.04594393, -0.57829812],\n",
       "       ...,\n",
       "       [ 1.        ,  0.3919669 ,  0.54981237, ...,  0.26650729,\n",
       "        -0.20275731,  1.20302531],\n",
       "       [ 1.        , -1.8355861 , -1.8360023 , ..., -1.04551839,\n",
       "        -1.13963133, -1.14082131],\n",
       "       [ 1.        , -1.35825331, -1.35883937, ...,  2.98427476,\n",
       "         3.26367657,  1.76554849]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# testdata = pd.read_csv('gdrive/My Drive/hw1-regression/test.csv', header = None, encoding = 'big5')\n",
    "testdata = pd.read_csv('work/hw1_data/test.csv', header = None, encoding = 'big5')\n",
    "test_data = testdata.iloc[:, 2:]\n",
    "test_data[test_data == 'NR'] = 0\n",
    "test_data = test_data.to_numpy()\n",
    "test_x = np.empty([240, 18*9], dtype = float)\n",
    "for i in range(240):\n",
    "    test_x[i, :] = test_data[18 * i: 18* (i + 1), :].reshape(1, -1)\n",
    "for i in range(len(test_x)):\n",
    "    for j in range(len(test_x[0])):\n",
    "        if std_x[j] != 0:\n",
    "            test_x[i][j] = (test_x[i][j] - mean_x[j]) / std_x[j]\n",
    "test_x = np.concatenate((np.ones([240, 1]), test_x), axis = 1).astype(float)\n",
    "test_x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **预测**\n",
    "说明图同上\n",
    "\n",
    "![alt text](https://ai-studio-static-online.cdn.bcebos.com/fc702c460628481889782e6a8f6490ce3e892c780e68481dbbb426dc9f459b31)\n",
    "\n",
    "有了 权重 和测试资料即可预测 目标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5.17496040e+00],\n",
       "       [ 1.83062143e+01],\n",
       "       [ 2.04912181e+01],\n",
       "       [ 1.15239429e+01],\n",
       "       [ 2.66160568e+01],\n",
       "       [ 2.05313481e+01],\n",
       "       [ 2.19065510e+01],\n",
       "       [ 3.17364687e+01],\n",
       "       [ 1.33916741e+01],\n",
       "       [ 6.44564665e+01],\n",
       "       [ 2.02645688e+01],\n",
       "       [ 1.53585761e+01],\n",
       "       [ 6.85894728e+01],\n",
       "       [ 4.84281137e+01],\n",
       "       [ 1.87023338e+01],\n",
       "       [ 1.01885957e+01],\n",
       "       [ 3.07403629e+01],\n",
       "       [ 7.11322178e+01],\n",
       "       [-4.13051739e+00],\n",
       "       [ 1.82356940e+01],\n",
       "       [ 3.85789223e+01],\n",
       "       [ 7.13115197e+01],\n",
       "       [ 7.41034816e+00],\n",
       "       [ 1.87179553e+01],\n",
       "       [ 1.49372503e+01],\n",
       "       [ 3.67197367e+01],\n",
       "       [ 1.79616970e+01],\n",
       "       [ 7.57894629e+01],\n",
       "       [ 1.23093102e+01],\n",
       "       [ 5.62953517e+01],\n",
       "       [ 2.51131609e+01],\n",
       "       [ 4.61024867e+00],\n",
       "       [ 2.48377055e+00],\n",
       "       [ 2.47594223e+01],\n",
       "       [ 3.04802805e+01],\n",
       "       [ 3.84639307e+01],\n",
       "       [ 4.42023106e+01],\n",
       "       [ 3.00868360e+01],\n",
       "       [ 4.04736750e+01],\n",
       "       [ 2.92264799e+01],\n",
       "       [ 5.60645605e+00],\n",
       "       [ 3.86660161e+01],\n",
       "       [ 3.46102134e+01],\n",
       "       [ 4.83896975e+01],\n",
       "       [ 1.47572477e+01],\n",
       "       [ 3.44668201e+01],\n",
       "       [ 2.74831069e+01],\n",
       "       [ 1.20008794e+01],\n",
       "       [ 2.13780362e+01],\n",
       "       [ 2.85444031e+01],\n",
       "       [ 2.01655138e+01],\n",
       "       [ 1.07966781e+01],\n",
       "       [ 2.21710358e+01],\n",
       "       [ 5.34462631e+01],\n",
       "       [ 1.22195811e+01],\n",
       "       [ 4.33009685e+01],\n",
       "       [ 3.21823351e+01],\n",
       "       [ 2.25672175e+01],\n",
       "       [ 5.67395142e+01],\n",
       "       [ 2.07450529e+01],\n",
       "       [ 1.50288546e+01],\n",
       "       [ 3.98553016e+01],\n",
       "       [ 1.29753407e+01],\n",
       "       [ 5.17416596e+01],\n",
       "       [ 1.87833696e+01],\n",
       "       [ 1.23487528e+01],\n",
       "       [ 1.56336237e+01],\n",
       "       [-5.88714707e-02],\n",
       "       [ 4.15080111e+01],\n",
       "       [ 3.15487475e+01],\n",
       "       [ 1.86042512e+01],\n",
       "       [ 3.74768197e+01],\n",
       "       [ 5.65203907e+01],\n",
       "       [ 6.58787719e+00],\n",
       "       [ 1.22293397e+01],\n",
       "       [ 5.20369640e+00],\n",
       "       [ 4.79273751e+01],\n",
       "       [ 1.30207057e+01],\n",
       "       [ 1.71103017e+01],\n",
       "       [ 2.06032345e+01],\n",
       "       [ 2.12844816e+01],\n",
       "       [ 3.86929353e+01],\n",
       "       [ 3.00207167e+01],\n",
       "       [ 8.87674067e+01],\n",
       "       [ 3.59847002e+01],\n",
       "       [ 2.67569136e+01],\n",
       "       [ 2.39635168e+01],\n",
       "       [ 3.27472428e+01],\n",
       "       [ 2.21890438e+01],\n",
       "       [ 2.09921589e+01],\n",
       "       [ 2.95559943e+01],\n",
       "       [ 4.09921689e+01],\n",
       "       [ 8.62511781e+00],\n",
       "       [ 3.23214718e+01],\n",
       "       [ 4.65980444e+01],\n",
       "       [ 2.28840708e+01],\n",
       "       [ 3.15181297e+01],\n",
       "       [ 1.11982335e+01],\n",
       "       [ 2.85274366e+01],\n",
       "       [ 2.91150680e-01],\n",
       "       [ 1.79669611e+01],\n",
       "       [ 2.71241639e+01],\n",
       "       [ 1.13982328e+01],\n",
       "       [ 1.64264269e+01],\n",
       "       [ 2.34252610e+01],\n",
       "       [ 4.06160827e+01],\n",
       "       [ 2.58641250e+01],\n",
       "       [ 5.42273695e+00],\n",
       "       [ 1.07949211e+01],\n",
       "       [ 7.28621369e+01],\n",
       "       [ 4.80228371e+01],\n",
       "       [ 1.57468083e+01],\n",
       "       [ 2.46704106e+01],\n",
       "       [ 1.28277933e+01],\n",
       "       [ 1.01580576e+01],\n",
       "       [ 2.72692233e+01],\n",
       "       [ 2.92087386e+01],\n",
       "       [ 8.83533962e+00],\n",
       "       [ 2.00510881e+01],\n",
       "       [ 2.02123337e+01],\n",
       "       [ 7.99060093e+01],\n",
       "       [ 1.80616143e+01],\n",
       "       [ 3.05428093e+01],\n",
       "       [ 2.59807924e+01],\n",
       "       [ 5.21257727e+00],\n",
       "       [ 3.03556973e+01],\n",
       "       [ 7.76832289e+00],\n",
       "       [ 1.53282683e+01],\n",
       "       [ 2.26663657e+01],\n",
       "       [ 6.27420542e+01],\n",
       "       [ 1.89507804e+01],\n",
       "       [ 1.90763556e+01],\n",
       "       [ 6.13715741e+01],\n",
       "       [ 1.58845621e+01],\n",
       "       [ 1.34094181e+01],\n",
       "       [ 8.48772484e-01],\n",
       "       [ 7.83499672e+00],\n",
       "       [ 5.70128290e+01],\n",
       "       [ 2.56079968e+01],\n",
       "       [ 4.96170473e+00],\n",
       "       [ 3.64148790e+01],\n",
       "       [ 2.87900067e+01],\n",
       "       [ 4.91941210e+01],\n",
       "       [ 4.03068699e+01],\n",
       "       [ 1.33161806e+01],\n",
       "       [ 2.76610119e+01],\n",
       "       [ 1.71580275e+01],\n",
       "       [ 4.96872626e+01],\n",
       "       [ 2.30302723e+01],\n",
       "       [ 3.92409365e+01],\n",
       "       [ 1.31967539e+01],\n",
       "       [ 5.94889370e+00],\n",
       "       [ 2.58216090e+01],\n",
       "       [ 8.25863421e+00],\n",
       "       [ 1.91463205e+01],\n",
       "       [ 4.31824865e+01],\n",
       "       [ 6.71784358e+00],\n",
       "       [ 3.38696152e+01],\n",
       "       [ 1.53699378e+01],\n",
       "       [ 1.69390450e+01],\n",
       "       [ 3.78853368e+01],\n",
       "       [ 1.92024845e+01],\n",
       "       [ 9.05950472e+00],\n",
       "       [ 1.02833996e+01],\n",
       "       [ 4.86724471e+01],\n",
       "       [ 3.05877162e+01],\n",
       "       [ 2.47740990e+00],\n",
       "       [ 1.28116039e+01],\n",
       "       [ 7.03247898e+01],\n",
       "       [ 1.48409677e+01],\n",
       "       [ 6.88655876e+01],\n",
       "       [ 4.27419924e+01],\n",
       "       [ 2.40002615e+01],\n",
       "       [ 2.34207249e+01],\n",
       "       [ 6.16721244e+01],\n",
       "       [ 2.54942028e+01],\n",
       "       [ 1.90048098e+01],\n",
       "       [ 3.48866829e+01],\n",
       "       [ 9.40231340e+00],\n",
       "       [ 2.95200113e+01],\n",
       "       [ 1.45739659e+01],\n",
       "       [ 9.12556314e+00],\n",
       "       [ 5.28125840e+01],\n",
       "       [ 4.50395380e+01],\n",
       "       [ 1.74524347e+01],\n",
       "       [ 3.84939353e+01],\n",
       "       [ 2.70389191e+01],\n",
       "       [ 6.55817097e+01],\n",
       "       [ 7.03730638e+00],\n",
       "       [ 5.27144771e+01],\n",
       "       [ 3.82064593e+01],\n",
       "       [ 2.11698011e+01],\n",
       "       [ 3.02475569e+01],\n",
       "       [ 2.71442299e+00],\n",
       "       [ 1.99329326e+01],\n",
       "       [-3.41333234e+00],\n",
       "       [ 3.24459994e+01],\n",
       "       [ 1.05829730e+01],\n",
       "       [ 2.17752257e+01],\n",
       "       [ 6.24652921e+01],\n",
       "       [ 2.41329437e+01],\n",
       "       [ 2.62012396e+01],\n",
       "       [ 6.37444772e+01],\n",
       "       [ 2.83429777e+00],\n",
       "       [ 1.43792470e+01],\n",
       "       [ 9.36985073e+00],\n",
       "       [ 9.88116661e+00],\n",
       "       [ 3.49494536e+00],\n",
       "       [ 1.22608049e+02],\n",
       "       [ 2.10835130e+01],\n",
       "       [ 1.75322206e+01],\n",
       "       [ 2.01830983e+01],\n",
       "       [ 3.63931322e+01],\n",
       "       [ 3.49351512e+01],\n",
       "       [ 1.88303127e+01],\n",
       "       [ 3.83445555e+01],\n",
       "       [ 7.79166341e+01],\n",
       "       [ 1.79532355e+00],\n",
       "       [ 1.34458279e+01],\n",
       "       [ 3.61311556e+01],\n",
       "       [ 1.51504035e+01],\n",
       "       [ 1.29418483e+01],\n",
       "       [ 1.13125241e+02],\n",
       "       [ 1.52246047e+01],\n",
       "       [ 1.48240260e+01],\n",
       "       [ 5.92673537e+01],\n",
       "       [ 1.05836953e+01],\n",
       "       [ 2.09930626e+01],\n",
       "       [ 9.78936588e+00],\n",
       "       [ 4.77118001e+00],\n",
       "       [ 4.79278069e+01],\n",
       "       [ 1.23994384e+01],\n",
       "       [ 4.81464766e+01],\n",
       "       [ 4.04663804e+01],\n",
       "       [ 1.69405903e+01],\n",
       "       [ 4.12665445e+01],\n",
       "       [ 6.90278920e+01],\n",
       "       [ 4.03462492e+01],\n",
       "       [ 1.43137440e+01],\n",
       "       [ 1.57707266e+01]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w = np.load('work/weight.npy')\n",
    "ans_y = np.dot(test_x, w)\n",
    "ans_y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# **保存预测到CSV文件**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['id', 'value']\n",
      "['id_0', 5.17496039898474]\n",
      "['id_1', 18.30621425352791]\n",
      "['id_2', 20.491218094180528]\n",
      "['id_3', 11.523942869805328]\n",
      "['id_4', 26.616056752306154]\n",
      "['id_5', 20.531348081761223]\n",
      "['id_6', 21.90655101879737]\n",
      "['id_7', 31.736468747068837]\n",
      "['id_8', 13.391674055111707]\n",
      "['id_9', 64.45646650291957]\n",
      "['id_10', 20.264568836159455]\n",
      "['id_11', 15.358576077361239]\n",
      "['id_12', 68.58947276926726]\n",
      "['id_13', 48.42811374745719]\n",
      "['id_14', 18.70233382419321]\n",
      "['id_15', 10.188595737466713]\n",
      "['id_16', 30.740362859820426]\n",
      "['id_17', 71.13221776355108]\n",
      "['id_18', -4.130517391262437]\n",
      "['id_19', 18.2356940164287]\n",
      "['id_20', 38.57892227500776]\n",
      "['id_21', 71.31151972531332]\n",
      "['id_22', 7.410348162634065]\n",
      "['id_23', 18.71795533032141]\n",
      "['id_24', 14.937250260084582]\n",
      "['id_25', 36.71973669470534]\n",
      "['id_26', 17.961697005662707]\n",
      "['id_27', 75.78946287210536]\n",
      "['id_28', 12.309310248614468]\n",
      "['id_29', 56.295351739649604]\n",
      "['id_30', 25.113160865661484]\n",
      "['id_31', 4.610248674094032]\n",
      "['id_32', 2.48377055451501]\n",
      "['id_33', 24.75942226132128]\n",
      "['id_34', 30.48028046559117]\n",
      "['id_35', 38.46393074642663]\n",
      "['id_36', 44.20231060933006]\n",
      "['id_37', 30.086836019866013]\n",
      "['id_38', 40.473675015740085]\n",
      "['id_39', 29.226479902317372]\n",
      "['id_40', 5.606456054343935]\n",
      "['id_41', 38.666016078789625]\n",
      "['id_42', 34.610213431877206]\n",
      "['id_43', 48.389697507384795]\n",
      "['id_44', 14.757247666944181]\n",
      "['id_45', 34.46682011087207]\n",
      "['id_46', 27.483106874184344]\n",
      "['id_47', 12.000879378154067]\n",
      "['id_48', 21.37803615160376]\n",
      "['id_49', 28.54440309166328]\n",
      "['id_50', 20.165513818411572]\n",
      "['id_51', 10.796678149746496]\n",
      "['id_52', 22.171035755750125]\n",
      "['id_53', 53.44626310935228]\n",
      "['id_54', 12.21958112161003]\n",
      "['id_55', 43.30096845517152]\n",
      "['id_56', 32.18233510328545]\n",
      "['id_57', 22.5672175145708]\n",
      "['id_58', 56.739514165547035]\n",
      "['id_59', 20.745052945295466]\n",
      "['id_60', 15.02885455747325]\n",
      "['id_61', 39.85530159038511]\n",
      "['id_62', 12.975340680728298]\n",
      "['id_63', 51.741659592830054]\n",
      "['id_64', 18.78336963253989]\n",
      "['id_65', 12.348752842777698]\n",
      "['id_66', 15.63362365354191]\n",
      "['id_67', -0.058871470684989546]\n",
      "['id_68', 41.50801107307595]\n",
      "['id_69', 31.548747530656023]\n",
      "['id_70', 18.604251157547075]\n",
      "['id_71', 37.47681972488072]\n",
      "['id_72', 56.52039065762305]\n",
      "['id_73', 6.587877193521961]\n",
      "['id_74', 12.229339737435051]\n",
      "['id_75', 5.203696404134665]\n",
      "['id_76', 47.92737510380061]\n",
      "['id_77', 13.02070568559468]\n",
      "['id_78', 17.11030169390362]\n",
      "['id_79', 20.60323453100203]\n",
      "['id_80', 21.284481560784613]\n",
      "['id_81', 38.69293529051181]\n",
      "['id_82', 30.02071667572584]\n",
      "['id_83', 88.76740666723549]\n",
      "['id_84', 35.98470023966829]\n",
      "['id_85', 26.756913553477187]\n",
      "['id_86', 23.963516843564452]\n",
      "['id_87', 32.74724282808311]\n",
      "['id_88', 22.18904375531993]\n",
      "['id_89', 20.992158853626545]\n",
      "['id_90', 29.555994316645446]\n",
      "['id_91', 40.992168866517815]\n",
      "['id_92', 8.625117809911558]\n",
      "['id_93', 32.321471808877895]\n",
      "['id_94', 46.5980443653676]\n",
      "['id_95', 22.884070826723555]\n",
      "['id_96', 31.518129728251647]\n",
      "['id_97', 11.19823347976611]\n",
      "['id_98', 28.5274366425296]\n",
      "['id_99', 0.29115068008962824]\n",
      "['id_100', 17.96696107953969]\n",
      "['id_101', 27.12416392947013]\n",
      "['id_102', 11.398232780652856]\n",
      "['id_103', 16.42642686567352]\n",
      "['id_104', 23.42526104692215]\n",
      "['id_105', 40.61608267056841]\n",
      "['id_106', 25.864125026560426]\n",
      "['id_107', 5.422736951672377]\n",
      "['id_108', 10.794921122256119]\n",
      "['id_109', 72.86213692992123]\n",
      "['id_110', 48.02283705948142]\n",
      "['id_111', 15.746808276903]\n",
      "['id_112', 24.670410614177975]\n",
      "['id_113', 12.82779332653672]\n",
      "['id_114', 10.158057570240516]\n",
      "['id_115', 27.269223342020968]\n",
      "['id_116', 29.20873857793243]\n",
      "['id_117', 8.835339619930767]\n",
      "['id_118', 20.051088137129774]\n",
      "['id_119', 20.212333743764244]\n",
      "['id_120', 79.90600929870558]\n",
      "['id_121', 18.061614288263602]\n",
      "['id_122', 30.542809341304327]\n",
      "['id_123', 25.980792377728037]\n",
      "['id_124', 5.212577268164771]\n",
      "['id_125', 30.355697305856204]\n",
      "['id_126', 7.7683228889146525]\n",
      "['id_127', 15.328268255393322]\n",
      "['id_128', 22.66636571769797]\n",
      "['id_129', 62.742054211090085]\n",
      "['id_130', 18.950780367988013]\n",
      "['id_131', 19.07635563083854]\n",
      "['id_132', 61.371574091637115]\n",
      "['id_133', 15.884562052629725]\n",
      "['id_134', 13.409418077705551]\n",
      "['id_135', 0.848772483611284]\n",
      "['id_136', 7.834996717304147]\n",
      "['id_137', 57.012829011796775]\n",
      "['id_138', 25.60799675181382]\n",
      "['id_139', 4.961704729242089]\n",
      "['id_140', 36.41487903906276]\n",
      "['id_141', 28.79000672197592]\n",
      "['id_142', 49.19412096197636]\n",
      "['id_143', 40.3068698557345]\n",
      "['id_144', 13.316180593982686]\n",
      "['id_145', 27.66101187522916]\n",
      "['id_146', 17.15802752436676]\n",
      "['id_147', 49.687262569296806]\n",
      "['id_148', 23.030272291604774]\n",
      "['id_149', 39.240936524842766]\n",
      "['id_150', 13.19675388941253]\n",
      "['id_151', 5.948893701039418]\n",
      "['id_152', 25.82160897630425]\n",
      "['id_153', 8.258634214291638]\n",
      "['id_154', 19.14632051722559]\n",
      "['id_155', 43.18248652651674]\n",
      "['id_156', 6.717843578093026]\n",
      "['id_157', 33.869615246810646]\n",
      "['id_158', 15.369937846981836]\n",
      "['id_159', 16.939044973551933]\n",
      "['id_160', 37.88533679463485]\n",
      "['id_161', 19.202484541054456]\n",
      "['id_162', 9.059504715654715]\n",
      "['id_163', 10.283399610648509]\n",
      "['id_164', 48.67244712569829]\n",
      "['id_165', 30.587716213230816]\n",
      "['id_166', 2.4774098975321452]\n",
      "['id_167', 12.811603937805929]\n",
      "['id_168', 70.32478980976462]\n",
      "['id_169', 14.840967694067054]\n",
      "['id_170', 68.86558756678863]\n",
      "['id_171', 42.741992444866355]\n",
      "['id_172', 24.000261542920168]\n",
      "['id_173', 23.420724860321446]\n",
      "['id_174', 61.67212443568237]\n",
      "['id_175', 25.49420284505919]\n",
      "['id_176', 19.004809786869075]\n",
      "['id_177', 34.886682881896846]\n",
      "['id_178', 9.402313398379729]\n",
      "['id_179', 29.52001131440802]\n",
      "['id_180', 14.573965885700478]\n",
      "['id_181', 9.125563143203577]\n",
      "['id_182', 52.81258399813188]\n",
      "['id_183', 45.039537994389626]\n",
      "['id_184', 17.452434679183302]\n",
      "['id_185', 38.49393527971432]\n",
      "['id_186', 27.038919092643827]\n",
      "['id_187', 65.58170967424581]\n",
      "['id_188', 7.03730638076959]\n",
      "['id_189', 52.71447713411569]\n",
      "['id_190', 38.20645933704979]\n",
      "['id_191', 21.169801059557855]\n",
      "['id_192', 30.247556879488396]\n",
      "['id_193', 2.7144229897163084]\n",
      "['id_194', 19.93293258764083]\n",
      "['id_195', -3.4133323376039124]\n",
      "['id_196', 32.44599940281316]\n",
      "['id_197', 10.582973029979941]\n",
      "['id_198', 21.77522570725846]\n",
      "['id_199', 62.46529206567788]\n",
      "['id_200', 24.13294368731647]\n",
      "['id_201', 26.201239647400943]\n",
      "['id_202', 63.744477234402886]\n",
      "['id_203', 2.834297774129025]\n",
      "['id_204', 14.379246986978824]\n",
      "['id_205', 9.369850731753873]\n",
      "['id_206', 9.881166613595408]\n",
      "['id_207', 3.494945358972141]\n",
      "['id_208', 122.60804937921779]\n",
      "['id_209', 21.08351301448056]\n",
      "['id_210', 17.53222059945512]\n",
      "['id_211', 20.183098344597003]\n",
      "['id_212', 36.393132212281856]\n",
      "['id_213', 34.93515120529069]\n",
      "['id_214', 18.830312661458624]\n",
      "['id_215', 38.344555522723326]\n",
      "['id_216', 77.91663413807038]\n",
      "['id_217', 1.7953235508882321]\n",
      "['id_218', 13.445827939135775]\n",
      "['id_219', 36.13115559041213]\n",
      "['id_220', 15.150403498166298]\n",
      "['id_221', 12.941848334417898]\n",
      "['id_222', 113.12524093786388]\n",
      "['id_223', 15.22460467793436]\n",
      "['id_224', 14.824025968612066]\n",
      "['id_225', 59.26735368854046]\n",
      "['id_226', 10.583695290718476]\n",
      "['id_227', 20.993062563532167]\n",
      "['id_228', 9.789365880830388]\n",
      "['id_229', 4.771180008705969]\n",
      "['id_230', 47.92780690481286]\n",
      "['id_231', 12.39943839475105]\n",
      "['id_232', 48.14647656264414]\n",
      "['id_233', 40.46638039656413]\n",
      "['id_234', 16.94059027033295]\n",
      "['id_235', 41.266544489418735]\n",
      "['id_236', 69.02789203372902]\n",
      "['id_237', 40.34624924412242]\n",
      "['id_238', 14.313743982871172]\n",
      "['id_239', 15.770726634219796]\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "with open('work/submit.csv', mode='w', newline='') as submit_file:\n",
    "    csv_writer = csv.writer(submit_file)\n",
    "    header = ['id', 'value']\n",
    "    print(header)\n",
    "    csv_writer.writerow(header)\n",
    "    for i in range(240):\n",
    "        row = ['id_' + str(i), ans_y[i][0]]\n",
    "        csv_writer.writerow(row)\n",
    "        print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "以上打印的部分主要是为了看一下资料和结果的呈现，拿掉也无妨。另外，在自己的 linux 系统，可以将档案写死的的部分换成 sys.argv 的使用 (可在 终端 自行输入档案和档案位置)。\n",
    "最后，可以藉由调整 (learning_rate)学习率、iter_time (迭代次数)、取用特征的多少(取几个小时，取哪些特征栏位)，甚至是不同的 模型来超越基线。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.0.0b0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
